package cn.flying.cloud.flow.core.listener.event.callback;

import javax.annotation.Resource;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Map;

import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import cn.flying.cloud.flow.bo.FwProcInstBo;
import cn.flying.cloud.flow.core.constants.FlowableConstant;
import cn.flying.cloud.flow.core.enums.FlowStateEnum;
import cn.flying.cloud.flow.core.enums.TaskStateEnum;
import cn.flying.cloud.flow.service.FwProcInstService;

/**
 * 流程完成结束监听
 *
 * @author: admin
 * @date: 2023年02月26日 08:25
 * @version: 1.0
 */
@Component
public class ProcessCompletedListenerCallback implements EventListenerCallback {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private FwProcInstService procInstService;
    @Resource
    private RuntimeService runtimeService;

    @Override
    public void execute(FlowableEvent event) {
        logger.info("==========流程完成监听器回调处理---start===========");
        logger.info("监听事件类型：{}", event.getType().name());
        FlowableEntityEventImpl entityEvent = (FlowableEntityEventImpl) event;
        ExecutionEntityImpl entity = (ExecutionEntityImpl) entityEvent.getEntity();

        Map<String, Object> variables = entity.getVariables();
        Object o = variables.get(FlowableConstant.TaskState);
        // 流程完成，审批完成
        FwProcInstBo inst = new FwProcInstBo();
        inst.setProcInstId(entity.getProcessInstanceId());
        inst = procInstService.getOneObj(inst);
        inst.setEndTime(LocalDateTime.now());
        inst.setDuration(String.valueOf(Duration.between(inst.getStartTime(), inst.getEndTime()).toMillis()));
        if (TaskStateEnum.A.getCode().equals(o)) {
            inst.setProcStatus(FlowStateEnum.Success.getCode());
            runtimeService.updateBusinessStatus(entityEvent.getProcessInstanceId(), FlowStateEnum.Success.getCode());
        } else if (TaskStateEnum.R.getCode().equals(o)) {
            inst.setProcStatus(FlowStateEnum.Refuse.getCode());
            runtimeService.updateBusinessStatus(entityEvent.getProcessInstanceId(), FlowStateEnum.Refuse.getCode());
        } else {
            inst.setProcStatus(FlowStateEnum.Reject.getCode());
            runtimeService.updateBusinessStatus(entityEvent.getProcessInstanceId(), FlowStateEnum.Reject.getCode());
        }
        inst.setRemark((String) variables.get(FlowableConstant.COMMENT));
        procInstService.updateObj(inst);

        ExecutionEntityImpl parent = entity.getParent();
        logger.info("流程【{}】处理完成，实例ID：【{}】，业务流水号：【{}】，", entity.getProcessDefinitionId(), entity.getProcessInstanceId(), entity.getBusinessKey());
        logger.info("==========流程完成监听器回调处理---end===========");
    }
}
